//
//  MovieNineViewController.m
//  UI_豆瓣2.0
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "MovieNineViewController.h"
#import "MovieNineCell.h"
#import "MovieDetailViewController.h"
#import "AppTools.h"
#import "MJRefresh.h"
#import "MovieList.h"
#import "DataBaseSingleton.h"

@interface MovieNineViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain)UICollectionView *nineCV;
@property (nonatomic, assign) BOOL isUpLoad;
@property (nonatomic, retain)NSMutableArray *nineArray;

@end

@implementation MovieNineViewController

- (void)dealloc
{
    [_nineArray release];
    [_nineCV release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UICollectionViewFlowLayout *flowL = [[UICollectionViewFlowLayout alloc] init];
    self.nineCV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 50) collectionViewLayout:flowL];
// 最小行间距
    flowL.minimumLineSpacing = 20;
// 单元大小
    flowL.itemSize = CGSizeMake(WIDTH - 280, HEIGHT - 507);
// 与屏幕四边间距
    flowL.sectionInset = UIEdgeInsetsMake(25, 20, 25, 20);
    
    self.nineCV.delegate = self;
    self.nineCV.dataSource = self;
    self.nineCV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.nineCV];
    [_nineCV release];
#pragma mark -- 注册
    [self.nineCV registerClass:[MovieNineCell class] forCellWithReuseIdentifier:@"cell"];
#pragma mark -- 调用上拉下拉方法
    [self addHeaderRefresh];
    [self addFooterRefresh];
    // 创建接收数组
    self.nineArray = [NSMutableArray array];
#pragma mark -- 数据库查询
    NSMutableArray *array = [DATABASE selectMovie];
    self.nineArray = [NSMutableArray arrayWithArray:array];
    [self.nineCV reloadData];
// 默认不是上拉
    self.isUpLoad = NO;
    
}


#pragma mark -- 设置下拉刷新
-(void)addHeaderRefresh
{
    //当下拉时候会触发该方法
    [self.nineCV addHeaderWithCallback:^{
        
        NSLog(@"下拉");
        self.isUpLoad = NO;
        // 重新请求数据
        [self getAFNetWorkingData];
        
    }];
    // 进到应用自动刷新(可以放在viewdidload)
    [self.nineCV headerBeginRefreshing];
}
#pragma mark -- 设置上拉刷新
-(void)addFooterRefresh
{
    //当上拉时候触发该方法
    [self.nineCV addFooterWithCallback:^{
        
        NSLog(@"上拉");
        self.isUpLoad = YES;
        // 重新请求数据
        [self getAFNetWorkingData];
    }];
}
#warning Block封装AFN,请求数据,并将数据存入model中
- (void)getAFNetWorkingData{
    NSString *url_string = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php"];
    [AppTools getDataWithURL:url_string Block:^(id result) {
        
        if (self.isUpLoad == NO) {
            [self.nineArray removeAllObjects];
            NSLog(@"清空数组");
        } else {
            NSLog(@"不用清空数组");
        }
        NSMutableDictionary *bigDic = result;
        NSMutableArray *array = [bigDic objectForKey:@"result"];
        for (NSMutableDictionary *dic in array) {
            MovieList *movieL = [[MovieList alloc] init];
            [movieL setValuesForKeysWithDictionary:dic];
            [self.nineArray addObject:movieL];
            [DATABASE deleteMovieId:movieL.movieId];
            [DATABASE insertMovie:movieL];
            [movieL release];
        }
#pragma mark -- 结束下拉上拉刷新
        [self.nineCV headerEndRefreshing];
        [self.nineCV footerEndRefreshing];
        [self.nineCV reloadData];
    }];
}

#pragma mark -- 设置区
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
#pragma mark -- 设置item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.nineArray.count;
}
#pragma mark -- 自定义cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdntifier = @"cell";
    MovieNineCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdntifier forIndexPath:indexPath];
    
    MovieList *movieL = [self.nineArray objectAtIndex:indexPath.row];
    cell.movieLNine = movieL;
    
    return cell;
}
#pragma mark -- 选择某个cell
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
// 隐藏详情界面的TabBar
    MovieDetailViewController *movieDetailVC = [[MovieDetailViewController alloc] init];
    [movieDetailVC setHidesBottomBarWhenPushed:YES];
    
    MovieList *movieL = [self.nineArray objectAtIndex:indexPath.row];
    movieDetailVC.movieL = movieL;
    
    [self.navigationController pushViewController:movieDetailVC animated:YES];
    [movieDetailVC release];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
